<template>
    <div class="goods-sku" v-if="goodsList">
        <dl v-for="item in goodsList.specs">
            <dt>{{ item.name }}</dt>
            <dd>
                <template v-for="item2 in item.values">
                    <img :class="{ selected: item2.active, disabled: item2.disabled }" :src="item2.picture"
                        :title="item2.name" v-if="item2.picture" @click="selectAttribute(item, item2)">
                    <span :class="{ selected: item2.active, disabled: item2.disabled }" v-else
                        @click="selectAttribute(item, item2)">{{ item2.name
                        }}</span>
                </template>
            </dd>
        </dl>
    </div>
</template>
<script>
export default {
    name: 'GoodsSku'
}
</script>

<script setup>
import { inject, onUnmounted } from 'vue'
import powerSet from '@/vender/power-set.js'
const goodsList = inject('goodsList')
const star = '*'
const emits = defineEmits(['change'])
const props = defineProps(['skuId'])

const initSelectedSkus = (goodsList) => {
    const skuObj = goodsList.skus.find(sku => sku.id === props.skuId)
    if (skuObj) {
        goodsList.specs.forEach((spec, index) => {
            const name = skuObj.specs[index].valueName

            spec.values.forEach(item => item.active = item.name === name)
        })
    }
    console.log(skuObj);
}

initSelectedSkus(goodsList.value)


const getSelectedSkus = (specs) => {
    const selectedArr = []
    specs.forEach(spec => {
        const obj = spec.values.find(val => val.active === true)
        selectedArr.push(obj?.name)
    })

    return selectedArr
}

const getPathMap = (skus) => {
    const pathMap = {}
    skus.forEach(sku => {
        if (sku.inventory) {
            const valueNameArr = sku.specs.map(item => item.valueName)
            const skuArr = powerSet(valueNameArr)
            skuArr.forEach(s => {
                const key = s.join(star)
                if (!pathMap[key]) pathMap[key] = []
                pathMap[key].push(sku.id)
            })
        }
    })

    return pathMap
}

const updateDisableStatus = (specs) => {
    specs.forEach((spec, index) => {
        const selectedSkus = getSelectedSkus(goodsList.value.specs)
        spec.values.forEach(val => {
            if (val.active) return
            selectedSkus[index] = val.name
            const key = selectedSkus.filter(x => x).join(star)
            val.disabled = !pathMap[key]
        })
    })
}

onUnmounted(() => {
    console.log('GOOdsSku');
})

let pathMap = getPathMap(goodsList.value.skus)
updateDisableStatus(goodsList.value.specs)

const selectAttribute = (parent, currItem) => {
    // console.log(pathMap);
    if (currItem.disabled) return
    parent.values.forEach(item => {
        if (item.name !== currItem.name) {
            item.active = false
        }
    })
    if (!currItem.active) {
        currItem.active = true

        const selectedSkus = getSelectedSkus(goodsList.value.specs)
        if (selectedSkus.includes(undefined)) {
            emits('change', {})
        } else {
            const key = selectedSkus.join(star)
            const [skuId] = pathMap[key]
            const { inventory, oldPrice, price, specs } = goodsList.value.skus.find(sku => sku.id === skuId)
            emits('change', {
                skuId,
                inventory,
                oldPrice,
                price,
                attrsText: specs.map(x => `${x.name}:${x.valueName}`).join(' ')
            })
        }
    } else {
        currItem.active = false
    }

    updateDisableStatus(goodsList.value.specs)
}

</script>

<style scoped lang="less">
.sku-state-mixin () {
    border: 1px solid #e4e4e4;
    margin-right: 10px;
    cursor: pointer;

    &.selected {
        border-color: @xtxColor;
    }

    &.disabled {
        opacity: 0.6;
        border-style: dashed;
        cursor: not-allowed;
    }
}

.goods-sku {
    padding-left: 10px;
    padding-top: 20px;

    dl {
        display: flex;
        padding-bottom: 20px;
        align-items: center;

        dt {
            width: 50px;
            color: #999;
        }

        dd {
            flex: 1;
            color: #666;

            >img {
                width: 50px;
                height: 50px;
                .sku-state-mixin ();
            }

            >span {
                display: inline-block;
                height: 30px;
                line-height: 28px;
                padding: 0 20px;
                .sku-state-mixin ();
            }
        }
    }
}
</style>